module AniadirNuevoCorteWindow (aniadirNuevoCorteWindow) where

{-
AUTOR:Gaston Charkiewicz
DESCRIPCION: La ventana de añadir un nuevo corte a realizarse, a un usuario en particular.
OBSERVACIONES:
-}

import Graphics.UI.Gtk
import MainHandler
import Customer
import Data.IORef
import Haircut
import WindowUtils
import PerformedHaircut

aniadirNuevoCorteWindow :: Customer -> IORef MainHandler -> IO()
aniadirNuevoCorteWindow customer mainHandler  = do
  initGUI
  window <- windowNew
  window `onDestroy` mainQuit
  windowSetPosition window WinPosCenter
  
  -- Label de fantasia para que no quede pegado arriba
  fantasiaLabel <- labelNew (Just "")
  
  -- Para empaquetar, una principal y una auxiliar
  vbox <- vBoxNew False 10
  hbox <- hBoxNew True 4
  -- El combo
  combo <- comboHaircuts mainHandler
  -- El label Observaciones
  lab <- labelNew (Just "Observaciones:")
  -- El textField para las observaciones
  txtfield <- entryNew
  buttonAceptar <- buttonNewWithLabel "Aceptar"
  -- Crear uno nuevo 
  onClicked buttonAceptar ((addPerfomedHaircutToCustomer customer (getSelectedHaircut combo mainHandler) (entryGetText txtfield) mainHandler) window)
  
  -- Boton Volver atras
  buttonVolver <- buttonNewWithLabel "Volver"
  onClicked buttonVolver (widgetDestroy window)
  
  -- Seteo tipificaciones de la ventana
  set window [windowTitle := "Añadir corte a Cliente",windowDefaultWidth := 300,
        windowDefaultHeight := 250, containerBorderWidth := 10, containerChild := vbox]
  
  -- Empaquetado de la hBox auxiliar
  boxPackStart hbox buttonAceptar PackGrow 1  
  boxPackStart hbox buttonVolver PackGrow 1 
  
  -- Empaquetado principal
  boxPackStart vbox fantasiaLabel PackNatural 1
  boxPackStart vbox combo PackNatural 1
  boxPackStart vbox lab PackNatural 1
  boxPackStart vbox txtfield PackNatural 1
  boxPackEnd vbox hbox PackNatural 1
  
  -- Finalmente, muestro todo
  widgetShowAll window
  mainGUI
  
---------------------------------------------------------------------------
-----------------FUNCIONES DE CONTROL--------------------------------------
---------------------------------------------------------------------------

comboHaircuts db = do
 
  handler <- readIORef db
  store <- listStoreNew (getBaseHaircuts handler)
  combo <- comboBoxEntryNewWithModel store
  customStoreSetColumn store textColumn (getHaircutName)
  comboBoxEntrySetTextColumn combo textColumn
  
  return combo

textColumn :: ColumnId Haircut String
textColumn = makeColumnIdString 0 

--------------------------------------------------------------------------

addPerfomedHaircutToCustomer :: Customer -> IO Haircut -> IO String -> IORef MainHandler -> Window -> IO ()
addPerfomedHaircutToCustomer theCustomer haircut description ref win = do
                                                                   theHaircut <- haircut 
                                                                   theHaircutDesc <- description
                                                                   let newPH = (createPerformedHaircut theHaircut theHaircutDesc)
                                                                   let newCtm = addPerformedHaircut theCustomer newPH
                                                                   -------MODIFICO IOREF--------------------
                                                                   modifyIORef ref (updateCustomerInHandler newCtm)  
                                                                   -----------------------------------------
                                                                   addMsg <- messageDialogNew (Just win) [DialogDestroyWithParent] MessageInfo ButtonsOk "Corte agregado"
                                                                   dialogRun addMsg
    
                                                                   widgetDestroy win
                                                                   return ()
                                                                        
getSelectedHaircut :: ComboBoxEntry -> IORef MainHandler -> IO Haircut                                               
getSelectedHaircut combo db = do
    index <- comboBoxGetActive combo
    --int <- index
    handler <- readIORef db
    return (searchByIndex index (getBaseHaircuts handler))
    
updateCustomerInHandler :: Customer -> MainHandler -> MainHandler
updateCustomerInHandler ctm handler = let ctmList = getCustomers handler
                                        in setNewCustomerList handler (replaceCustomer ctmList ctm)
                                         
replaceCustomer :: [Customer] -> Customer -> [Customer]
replaceCustomer (ctm:ctms) customer = if (getCustomerName ctm == getCustomerName customer)
                                          then (customer:ctms)
                                          else ctm:(replaceCustomer ctms customer)
                                          

----------------------------------------------------------------------------------------
